<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>订单列表</title>
		<link rel="stylesheet" type="text/css" href="../resource/css/base.css">
		<script type="text/javascript" src="../resource/js/My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" src="../resource/js/popup/popup.js" charset="utf-8"></script>
		<script type="text/javascript" src="../resource/js/prototype.js"></script>
		<s:actionmessage/>
		<script type="text/javascript">
			function $(name){return document.getElementsByName(name);}
			function $$(name){return document.getElementById(name);}

			function setTotalFee(){
				var unitPrice = $('unitPrice')[0].value;
				var cargoQuantity = $('cargoQuantity')[0].value;
				$$('readOnlyUnitPrice').value = unitPrice;
				try{
					var unitPrice = $('unitPrice')[0].value;
					var cargoQuantity = $('cargoQuantity')[0].value;
					$$('totalReadOnly').innerText = Number(unitPrice) * Number(cargoQuantity);
				}catch(e){
					$$('totalReadOnly').innerText = 0;
				}
			}
			//弹出窗口选择客户
			function show(mark){//mark为区别发货(S)、签收(R)客户的标记
				ShowIframe('客户查询','./popupLoadClient.action?mark='+mark,600,395);
			}
			//回填客户选择信息
			function setForm(data){
				if(data != null && typeof data != "undefined"){
					//获取选择客户类型的标记
					var m = data["mark"];
					//设置隐藏域的客户ID,及其他信息点
					$(m+"ClientId")[0].value = data["items"][0].id;
					$(m+"name")[0].value = data["items"][0].cols[2];
					$(m+"ClientName")[0].value = data["items"][0].cols[1];
					$(m+"ClientPhone")[0].value = data["items"][0].cols[4];
					$(m+"Address")[0].value = data["items"][0].cols[3];
				}
			}
			//弹出窗口选择车辆信息
			function showTruck(){
				ShowIframe('车辆选择','./popupLoadTruck.action?',600,395);
			}
			//回填车辆选择信息
			function setTruck(data){
				if(data.drivers){//如果返回数据中含有drivers，则为自有车辆
					$("truckInfo")[0].value = data.truckId + "[" + data.driverNames + "]";
					$("_truckId")[0].value = data.truckId;
					$("_driverNames")[0].value = data.driverNames;
				}
				else{
					$("truckInfo")[0].value = data.truckId;
					$("_truckId")[0].value = data.truckId;
					$("_driverNames")[0].value = "-";
					//$("loadPertime")[0].value = data.cargoQuantity;
					$("b_note")[0].value = data.note;
					$("b_ownName")[0].value = data.ownName;
					$("b_unitFee")[0].value = data.unitFee;
					$("b_entryTime")[0].value = data.entryTime;
				}
				
			}
			//添加运输方案行
			function addRows(){
				//数据验证——自己的车
				if($("_truckId")[0].value==""){
					alert("请选择派发车辆.");
					return false;
				}
				if($("_driverNames")[0].value==""){
					alert("请选择驾驶员或者车主.");
					return false;
				}
				if($("loadPertime")[0].value==""){
					alert("请填写每次运货量.");
					return false;
				}
				if($("unitPrice")[0].value==""){
					alert("请填写运货单价.");
					return false;
				}
				var pattern = /^[0-9]+(\.[0-9]{1,2}){0,1}$/;
				if (!pattern.test($("loadPertime")[0].value)){
					alert("每次运货量格式有误.请填写数字.");
					return false;
				}
				if (!pattern.test($("unitPrice")[0].value)){
					alert("运货单价格式有误.请填写数字.");
					return false;
				}
				var table = $$("plan");
				var row = table.insertRow(table.rows.length - 1);
				row.style.cssText = "text-align: center;background-color: #FFFFFF;"
				var cell = row.insertCell();
				cell.innerText = table.rows.length - 2;
				cell = row.insertCell();
				cell.innerText = $("_truckId")[0].value;
				cell = row.insertCell();
				cell.innerText = $("_driverNames")[0].value;
				cell = row.insertCell();
				cell.innerText = $("loadPertime")[0].value;
				cell = row.insertCell();
				cell.innerText = $("unitPrice")[0].value;
				cell = row.insertCell();
				cell.innerText = $("transTimes")[0].value;
				cell = row.insertCell();
				cell.innerText = Number($("loadPertime")[0].value) * Number($("unitPrice")[0].value) * Number($("transTimes")[0].value);
				cell = row.insertCell();
				cell.innerHTML = "<input type=\"hidden\" name=\"cellValue\" value=\""
								+ $("_truckId")[0].value + "@" 
								+ $("_driverNames")[0].value + "@" 
								+ $("loadPertime")[0].value + "@"
								+ $("transTimes")[0].value + "@" 
								+ $("b_ownName")[0].value + "@"
								+ $("b_unitFee")[0].value + "@"
								+ $("b_note")[0].value + "@"
								+ $("b_entryTime")[0].value + " \" />";
				cell.innerHTML += "<input type=\"button\" style=\"width:30px;\" onclick=\"delRows(this);\" value=\"删除\"/>";
				rebuildTable();
				$("truckInfo")[0].value = "";
				$("_truckId")[0].value = "";
				$("_driverNames")[0].value = "";
				$("loadPertime")[0].value = "";
				$("transTimes")[0].value = 1;
				$("b_entryTime")[0].value = "";
			}
			function rebuildTable(){
				var table = $$("plan");
				var totalTimes = 0, totalQuality = 0, totalFeeRead = 0;
				for(var i = 1; i < table.rows.length - 1; i++){
					table.rows[i].cells[0].innerText = i;
					totalTimes += Number(table.rows[i].cells[5].innerText);
					totalQuality += Number(table.rows[i].cells[5].innerText) * Number(table.rows[i].cells[3].innerText);
					totalFeeRead += Number(table.rows[i].cells[6].innerText);
				}
				$$("totalTimesStr").innerText = totalTimes;
				$$("totalQualityStr").innerText = totalQuality;
				$$("totalFeeStr").innerText = totalFeeRead;
			}
			function delRows(obj){
				var table = $$("plan");
				var index = obj.parentNode.parentNode.cells[0].innerText;
				table.deleteRow(index);
				rebuildTable();
			}
			//添加
			function sumbit(){
				//判断数据是否填写及正确与否
				var form = document.forms[0];
				var sname = document.getElementById("Sname").value;
				var rname = document.getElementById("Rname").value;
				var reg = /^((\d{11})|^((\d{7,8})|(\d{3,4})-(\d{7,8})|(\d{3,4})-(\d{7,8})-(\d{1,4})|(\d{7,8})-(\d{1,4}))$)$/;
				var pattern = /^[0-9]+(\.[0-9]{1,2}){0,1}$/;
				var phoneS = $("SClientPhone")[0].value;
				var phoneR = $("RClientPhone")[0].value;
				var unitPrice = $("unitPrice")[0].value;
				var cargoQuantity = $("cargoQuantity")[0].value;
				if(sname == ""){
					alert("请选择发货方单位.");
					return false;
				}
				if(rname == ""){
					alert("请选择签收方单位.");
					return false;
				}
				if(form.SClientName.value == ""){
					alert("请填写发件人.");
					return false;
				}
				if(form.SClientPhone.value == ""){
					alert("请填写发件人联系电话.");
					return false;
				}
				if(reg.test(phoneS) == false){
					alert("发件人联系电话格式不正确.");
					return false;
				}
			    if(form.RClientName.value == ""){
			    	alert("请填写签收人.");
			    	return false;
			    }			 
			    if(form.RClientPhone.value == ""){
			    	alert("请填写签收人联系电话.");
			    	return false;
			    }
			    if(reg.test(phoneR) == false){
			    	alert("签收人联系电话格式不正确.");
			    	return false;
			    }
			    if(form.cargoName.value == ""){
			    	alert("请填写货物名称.");
			    	return false;
			    }
			    if(form.cargoQuantity.value == ""){
			    	alert("请填写货物数量.");
			    	return false;
			    }
			    if (!pattern.test(form.cargoQuantity.value)){
					alert("货物数量格式有误.请填写数字");
					return false;
				}
			    if(form.unitPrice.value == ""){
			    	alert("请填写货物单价.");
			    	return false;
			    }
			    if (!pattern.test(form.unitPrice.value)){
					alert("货物单价格式有误.请填写数字");
					return false;
				}
		    //判断付款方式
			    if($("S")[0].checked && $("r")[0].checked){
					$("chargeType")[0].value = 0;
					if(form.SCharge.value == ""){
						alert("请填写发货方付款金额.");
						return false;
					}
					if (!pattern.test(form.SCharge.value)){
						alert("付款金额格式有误.请填写数字");
						return false;
					}
					if(form.RCharge.value == ""){
			    		alert("请填写签收方付款金额.");
			    		return false;
			    	}
			    	if (!pattern.test(form.RCharge.value)){
						alert("付款金额格式有误.请填写数字");
						return false;
					}
					if($("SnextChargeDate")[0].value==""){
						alert("请填写发货方预付日期.");
						return false;
					}
					if($("RnextChargeDate")[0].value==""){
						alert("请填写签收方预付日期.");
						return false;
					}
			    }
			    else if($("S")[0].checked){
			    	$("chargeType")[0].value=1;
			    	if(form.SCharge.value == ""){
			    		alert("请填写发货方付款金额.");
			    		return false;
			    	}
			    	if (!pattern.test(form.SCharge.value)){
						alert("付款金额格式有误.请填写数字");
						return false;
					}
					if($("SnextChargeDate")[0].value==""){
						alert("请填写发货方预付日期.");
						return false;
					}
			    }
			    else if($("R")[0].checked==true){
			    	$("chargeType")[0].value=2;
			    	if(form.RCharge.value == ""){
			    		alert("请填写签收方付款金额.");
			     		return false;
			    	}
			    	if (!pattern.test(form.RCharge.value)){
						alert("付款金额格式有误.请填写数字");
						return false;
					}
					if($("RnextChargeDate")[0].value==""){
						alert("请填写签收方预付日期.");
						return false;
					}
			    }
			    else{
			    	alert("请选择付款方式.");
			    	return false;
			    }
		    	//计算 总价（totalFee）= unitPrice × cargoQuantity
			    var dd = Number(unitPrice) * Number(cargoQuantity);
			    $("totalFee")[0].value = Number(unitPrice)*Number(cargoQuantity);
			    form.action="add.action";
				form.submit();
			}
			//勾选checkbox
			function checkboxed(boxName){
				var falg = $(boxName)[0].checked;
				if(falg==true){
					$(boxName+"Charge")[0].disabled=false;
					$(boxName+"nextChargeDate")[0].disabled=false;
				}
				else{
					$(boxName+"Charge")[0].disabled=true;
					$(boxName+"nextChargeDate")[0].disabled=true;
				}
			}
		</script>
		<style>
			<!--
			.edittb input{
				width: 92%;
			}
			.edittb input.notext{
				width: auto;
			}
			.coln{width: 30%;}
			.edittb input{
				width: 92%;
			}
			.edittb select{
				width: 92%;
			}
			.edittb textarea{
				width: 97%;
			}
			-->
		</style>
	</head>
	<body  oncontextmenu="return false;" leftmargin="8" topmargin="8">
		<div class="tip">
			<span>当前位置:</span>订单管理&nbsp;&gt;&nbsp;新增&nbsp;&gt;&nbsp;
		</div>
		<s:form name="infoForm" action="add" namespace="/invoiceOrder">
			<s:hidden name="chargeType"/>
			<s:hidden name="totalFee"/>
			<!-- 隐藏域，保存发货方、接收方客户ID -->
			<s:hidden name="SClientId"/>
			<s:hidden name="RClientId"/>
			<table class="edittb" cellpadding="2" cellspacing="1">
				<tr bgcolor="#EEF4EA">
					<td height="35" colspan="8">&nbsp;订单信息</td>
				</tr>
				<tr>
					<td width="50%" colspan="4" style="text-align: center;" class="colv">发&nbsp;货&nbsp;方</td>
					<td width="50%" colspan="4" style="text-align: center;" class="colv">签&nbsp;收&nbsp;方</td>
				</tr>
				<tr>
					<td class="coln"><SPAN style="color: red">*</SPAN> 单位名称</td>
					<td colspan="3" class="colv">
						<s:textfield id="Sname" name="Sname" readonly="true"/><img src="../resource/image/menusearch.gif" onclick="show('S');" style="cursor: pointer">
					</td>
					<td class="coln"><SPAN style="color: red">*</SPAN> 单位名称</td>
					<td colspan="3" class="colv">
						<s:textfield id="Rname" name="Rname" readonly="true"/><img src="../resource/image/menusearch.gif" onclick="show('R');" style="cursor: pointer">
					</td>
				</tr>
				<tr>
					<td class="coln"><SPAN style="color: red">*</SPAN> 发 件 人</td>
					<td class="colv">
						<s:textfield name="SClientName"/>
					</td>
					<td class="coln"><SPAN style="color: red">*</SPAN> 联系电话</td>
					<td class="colv">
						<s:textfield name="SClientPhone"></s:textfield>
					</td>
					<td class="coln"><SPAN style="color: red">*</SPAN> 签 收 人</td>
					<td class="colv">
						<s:textfield name="RClientName"></s:textfield>
					</td>
					<td class="coln"><SPAN style="color: red">*</SPAN> 联系电话</td>
					<td class="colv">
						<s:textfield name="RClientPhone"></s:textfield>
					</td>
				</tr>
				<tr>
					<td class="coln">单位地址</td>
					<td colspan="3" class="colv">
						<s:textarea name="SAddress"></s:textarea>
					</td>
					<td class="coln">单位地址</td>
					<td colspan="3" class="colv">
						<s:textarea name="RAddress"></s:textarea>
					</td>
				</tr>
				<tr bgcolor="#EEF4EA">
					<td colspan="8">货物信息</td>
				</tr>
				<tr>
					<td class="coln"><SPAN style="color: red">*</SPAN> 货物名称</td>
					<td colspan="3" class="colv">
						<s:textfield name="cargoName" cssStyle="width: 97%;"/>
					</td>
					<td class="coln">货物规格</td>
					<td colspan="3" class="colv">
						<s:textfield name="cargoNorm" cssStyle="width: 97%;"/>
					</td>
				</tr>
				<tr>
					<td class="coln"><SPAN style="color: red">*</SPAN> 货物数量</td>
					<td class="colv">
						<s:textfield name="cargoQuantity" onblur="setTotalFee();"/>
					</td>
					<td class="coln">单　　位</td>
					<td class="colv">
						<s:select list="paramter" listKey="id.paramKey" listValue="paramValue" name="unit"/>
					</td>
					<td class="coln"><SPAN style="color: red">*</SPAN> 单　　价</td>
					<td class="colv">
						<s:textfield name="unitPrice" onblur="setTotalFee();" />
					</td>
					<td class="coln">总 运 费</td>
					<td class="colv">&nbsp;<span id="totalReadOnly">0</span></td>
				</tr>
				<tr bgcolor="#EEF4EA">
					<td colspan="8">&nbsp;付款方式</td>
				</tr>
				<tr>
					<td class="coln">发货方付款</td>
					<td class="colv" colspan="3" nowrap="nowrap">
						<input type="checkbox" name="S" onclick="checkboxed('S');"  style="width: 20px;" />
						金额
						<s:textfield name="SCharge" cssStyle="width: 100px;" disabled="true"></s:textfield>
						预付日期
						<s:textfield onclick="WdatePicker();" cssClass="Wdate" name="SnextChargeDate" cssStyle="width: 100px;" disabled="true"></s:textfield>
					</td>
					<td class="coln">签收方付款</td>
					<td class="colv" colspan="3"  nowrap="nowrap">
						<input type="checkbox" name="R" onclick="checkboxed('R');"  style="width: 20px;" />
						金额
						<s:textfield name="RCharge" cssStyle="width: 100px;" disabled="true"></s:textfield>
						预付日期
						<s:textfield onclick="WdatePicker();" cssClass="Wdate" name="RnextChargeDate" cssStyle="width: 100px;" disabled="true"></s:textfield>
					</td>
				</tr>
				<tbody id="planPanle" style="display: none">
					<tr bgcolor="#EEF4EA">
						<td colspan="8">&nbsp;运输方案</td>
					</tr>
					<tr>
						<td colspan="8" class="colv">
							<table width="100%" border='0' cellpadding='1px;' cellspacing='0'>
								<tr>
									<td class="colnv">派发车辆</td>
									<td class="colv" style="width: 25%;" nowrap="nowrap">
										<s:textfield name="truckInfo" cssStyle="width: 85%;" readonly="true"/><img src="../resource/image/menusearch.gif" onclick="showTruck();" style="cursor: pointer;">
										<s:hidden name="_truckId"/>
										<s:hidden name="_driverNames"/>
										<s:hidden name="_driverIds"/>
										<s:hidden name="b_note"/>
										<s:hidden name="b_ownName"/>
										<s:hidden name="b_unitFee"/>
										<s:hidden name="b_entryTime"/>
									</td>
									<td class="colnv" nowrap="nowrap">每次运货量</td>
									<td class="colv" style="width:10%" nowrap="nowrap">
										<s:textfield name="loadPertime" id="loadPertime" cssStyle="width:80%;"/>&nbsp;吨 </td>
									<td class="colnv">运货单价</td>
									<td class="colv" style="width:8%"><s:textfield id="readOnlyUnitPrice" readonly="readonly"/></td>
									<td class="colnv">往返次数</td>
									<td class="colv">
										<s:select list="#{1:'1',2:'2',3:'3',4:'4',5:'5',6:'6'}" name="transTimes" cssStyle="width:50px;"/>
										&nbsp;<input type="button" style="width:30px;" onclick="return addRows();" value="添加"/>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="8" class="colv">
							<table id="plan" width="100%" border='0' cellpadding='1' cellspacing='1' style="background-color: #CBD8AC">
								<tr style="text-align: center;background-color: #FAFAF1;">
									<td width="5%;">编号</td>
									<td width="15%;">车牌号</td>
									<td width="15%;">驾驶员</td>
									<td width="10%;">每次运货量</td>
									<td width="10%;">货物单价</td>
									<td width="5%;">往返次数</td>
									<td width="10%;">运费</td>
									<td width="5%;">操作</td>
								</tr>
								<tfoot>
									<tr style="text-align: center;background-color: #FFFFFF;">
										<td>合计</td>
										<td colspan="2">
											总发车次数:<span id="totalTimesStr">0</span>
										</td>
										<td colspan="2">
											总运货数量:<span id="totalQualityStr">0</span>
										</td>
										<td colspan="2">
											总运费:<span id="totalFeeStr">0</span>
										</td>
										<td></td>
									</tr>
								</tfoot>
							</table>
						</td>
					</tr>
				</tbody>
				<tr align="right" bgcolor="#EEF4EA">
					<td height="35" colspan="8" align="center">
						<label><input onclick="javascript:document.getElementById('planPanle').style.display=this.checked?'block':'none';" class="notext" type="checkbox">制定运输方案</label>
						<a href="javascript:history.go(-1)" class="coolbg">&nbsp;返&nbsp;回&nbsp;</a>
						<a href="#" class="coolbg" onclick="return sumbit();">&nbsp;保&nbsp;存&nbsp;</a>
						<a href="javascript:document.forms[0].reset()" class="coolbg">&nbsp;重&nbsp;置&nbsp;</a>
					</td>
				</tr>
			</table>
		</s:form>
	</body>
</html>
